﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Imports System.ComponentModel

Partial Public Class frmAdminDepartments
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminDepartments_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        FillBranch()
        refreshList()
        Me.txtName.IsEnabled = False
        Me.txtProducts.IsEnabled = False
        Me.drpBranch.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub FillBranch()
        With Me.drpBranch
            .SelectedValuePath = "Branch_ID"
            .DisplayMemberPath = "Branch_Name"
            .ItemsSource = CType(CommonDataFunctions.BranchList, IListSource).GetList
        End With

    End Sub
    Private Sub refreshList()
        Me.grdDepartments.ItemsSource = CType(CommonDataFunctions.DepartmentList, IListSource).GetList
        Me.grdDepartments.Columns(0).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        Me.txtName.IsEnabled = True
        Me.txtProducts.IsEnabled = True
        Me.drpBranch.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtName.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtName.Text = String.Empty
        Me.txtProducts.Text = String.Empty
        Me.drpBranch.SelectedIndex = -1
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        Me.txtName.IsEnabled = False
        Me.txtProducts.IsEnabled = False
        Me.drpBranch.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        grdDepartments.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        ' ClearControls()
        Me.txtName.IsEnabled = True
        Me.txtProducts.IsEnabled = True
        Me.drpBranch.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub grdDepartemnts_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdDepartments.SelectionChanged

        If Me.grdDepartments.SelectedItems.Count = 0 Then
            Me.grdDepartments.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdTitles.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Dim selectedRow As DataRowView = grdDepartments.SelectedItem
            Dim departmentId As Integer = Convert.ToInt32(selectedRow(0))
            Dim sql As StringBuilder = New StringBuilder()
            With sql
                .Append("SELECT [Department_ID],[Department_Name] ,[Department_Products],[Department_BranchID] FROM [GLOBAL].[dbo].[L_Departments] WHERE [Department_ID] = ")
                .Append(departmentId)
            End With
            Dim departmentTable As DataTable = CommonDataFunctions.GetSQLDatatable(sql.ToString)
            Try
                Dim departmentRow As DataRow = departmentTable(0)
                Me.txtName.Text = departmentRow("Department_Name")
                Me.txtProducts.Text = departmentRow("Department_Products").ToString()
                Me.drpBranch.SelectedValue = departmentRow("Department_BranchID")
            Catch ex As Exception
                MsgBox("There is an error fetching the Department details from the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
            End Try
            Me.txtName.IsEnabled = True
            Me.txtProducts.IsEnabled = True
            Me.drpBranch.IsEnabled = True
            Me.btnSave.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            intSaveNeworEdit = 2
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtName.Text.Trim = String.Empty Then
            MsgBox("Please enter Department !")
            txtName.Focus()
            Exit Sub
        ElseIf txtProducts.Text.Trim = String.Empty Then
            MsgBox("Please enter Product !")
            txtName.Focus()
            Exit Sub
        ElseIf drpBranch.SelectedIndex = -1 Then
            MsgBox("Please select department !")
            Exit Sub
        Else
            btnSave.IsEnabled = False
            Dim sql As StringBuilder
            Dim selectedRowView As DataRowView = drpBranch.SelectedItem
            Dim branchId As Integer = Convert.ToInt32(selectedRowView(0))
            Dim product As Integer = Convert.ToInt32(txtProducts.Text.Trim)
            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder
                    With sql
                        .Append("")
                        .Append("insert into [GLOBAL].[dbo].[L_Departments]  ")
                        .Append("([Department_Name] ,[Department_Products]  ,[Department_BranchID])")
                        .Append(" Values(")
                        .Append(" '" & Me.txtName.Text.Trim() & "',")
                        .Append(product & ",")
                        .Append(branchId & ")")
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The new Department has been added successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error adding the Department to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If
                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update [GLOBAL].[dbo].[L_Departments]  set ")
                        .Append(" [Department_Name] = '" & Me.txtName.Text.Trim() & "',")
                        .Append(" [Department_Products] =" & product & ",")
                        .Append(" [Department_BranchID] = " & branchId)
                        Dim selectedRow As DataRowView = grdDepartments.SelectedItem
                        .Append(" where [Department_ID] = " & selectedRow(0))
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The Departments's details have been updated successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error in editing the Title's details. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If

            End Select
            ClearControls()
            txtName.IsEnabled = False
            txtProducts.IsEnabled = False
            drpBranch.IsEnabled = False
            grdDepartments.IsEnabled = True
            btnCancel.IsEnabled = False
            btnAddNew.IsEnabled = True
            btnEdit.IsEnabled = False
            btnDelete.IsEnabled = False
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete Department ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRowView = grdDepartments.SelectedItem
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete  from [GLOBAL].[dbo].[L_Departments] where [Department_ID] =" & selectedRow(0).ToString())
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdDepartments.IsEnabled = True
                ClearControls()
                refreshList()
                MsgBox("The Department Details have been deleted successfully.", MsgBoxStyle.Information, "Success")
            Else
                MsgBox("There is an error while deleting the Department from the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If

    End Sub
End Class
